﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BarberShop.BLL;


public partial class AdImgPlaySortAdd : System.Web.UI.Page
{
    public int Id
    {
        get
        {
            return string.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"]);
        }
    }
    public IList<int> AdIds
    {
        get
        {
            var list = AdPlayListRelationBLL.GetAdPalyListRe(Id);
            var r = (from a in list select a.AdInfo_ID.Value).ToList();
            return r;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAdData();
        }
    }

    protected void btn_search_Click(object sender, EventArgs e)
    {
        GetAdData();
    }

    //getData
    protected void GetAdData()
    {
        string name = tx_movieName.Text.Trim();
        var data = AdInfoBLL.GetAdList(name);
        this.Grid_Movie.DataSource = data;
        this.Grid_Movie.DataKeyNames = new string[] { "ID" };
        this.Grid_Movie.DataBind();
    }

    protected void Grid_Movie_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox cbox = (CheckBox)e.Row.Cells[0].FindControl("CheckBox1");
            if (AdIds.Contains((int)Grid_Movie.DataKeys[e.Row.RowIndex].Value))
            {
                cbox.Checked = true;
                cbox.Enabled = false;
            }
        }
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        string mids = "";
        for (int i = 0; i <= Grid_Movie.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)Grid_Movie.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true && cbox.Enabled == true)
            {
                int mid = (int)Grid_Movie.DataKeys[i].Value;
                mids += mid.ToString() + ",";
            }

        }
        AdPlayListRelationBLL.AddPalyList(Id, mids);
        GetAdData();
    }
}
